<template>
  <div class="app-container">

    <el-container>
      <el-header>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-row :gutter="20">
              <el-col :span="12">
                <el-input
                  v-model="kwInput"
                  placeholder="输入关键词进行筛选"
                  class="input-with-select"
                  clearable
                  @keyup.native.enter="handleSearch"
                >
                  <el-button slot="append" icon="el-icon-search" @click="handleSearch">搜索</el-button>
                </el-input>
              </el-col>
              <el-col :span="12">
                <el-button
                  type="success"
                  title="添加新账户"
                  icon="el-icon-plus"
                  @click="handleAction('create', '添加配置')"
                >新增
                </el-button>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
      </el-header>
      <el-container>
        <el-main>
          <el-table
            v-loading.body="listLoading"
            :data="data"
            border
            fit
            highlight-current-row
            :default-sort="{prop: 'create_at', order: 'descending'}"
            style="width: 100%"
            @sort-change="handleSort"
          >
            <el-table-column align="left" label="ID" prop="id" sortable="custom" width="50" />
            <el-table-column align="left" label="我的网站" prop="wpaccount_domain" sortable="custom" />
            <el-table-column align="left" label="plantform" prop="plantform" sortable="custom" />
            <el-table-column align="left" label="username" prop="username" sortable="custom" />
            <el-table-column align="left" label="password" prop="password" sortable="custom" />
            <el-table-column align="left" label="token" prop="token" sortable="custom" />
            <el-table-column align="left" label="today_post" prop="today_post" sortable="custom" />
            <el-table-column align="left" label="daily_post" prop="daily_post" sortable="custom" />
            <el-table-column align="center" label="操作" width="300" fixed="right">
              <template slot-scope="scope">
                <el-button
                  v-show="!scope.row.edit"
                  size="mini"
                  icon="el-icon-edit"
                  type="warning"
                  plain
                  @click="handleAction('edit', scope.row)"
                >修改
                </el-button>
                <el-button
                  v-show="!scope.row.edit"
                  size="mini"
                  icon="el-icon-delete"
                  type="danger"
                  plain
                  @click="handleAction('delete', scope.row)"
                >删除
                </el-button>
              </template>
            </el-table-column>
          </el-table>
          <el-pagination
            v-if="data.length"
            background
            class="mt-pagination"
            :current-page="page"
            :page-sizes="[10,20,50,100]"
            :page-size="pagesize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handlePageChange"
          />
        </el-main>
      </el-container>
    </el-container>
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="600px"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <el-form
        ref="dataForm"
        :model="temp"
        label-position="left"
        label-width="120px"
        style="margin-left: 8%"
      >
        <el-form-item label="推广平台" label-width="120px">
          <el-autocomplete v-model="temp.plantform" :fetch-suggestions="querySearchPlantform" placeholder="请输入内容" />
        </el-form-item>

        <el-form-item label="username" label-width="120px">
          <el-input v-model="temp.username" />
        </el-form-item>
        <el-form-item label="password" label-width="120px">
          <el-input v-model="temp.password" />
        </el-form-item>
        <el-form-item label="token" label-width="120px">
          <el-input v-model="temp.token" />
        </el-form-item>
        <el-form-item label="daily_post" label-width="120px">
          <el-input v-model="temp.daily_post" />
        </el-form-item>
        <el-form-item label="today_post" label-width="120px">
          <el-input v-model="temp.today_post" />
        </el-form-item>
        <el-form-item label="我的网站" label-width="120px">
          <single_wpaccount_select :wpaccount_id="temp.wpaccount_id" size="small" @change="changeWPAccount" />
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button :type="action !== 'delete'?'primary':'danger'" @click="confirm">确 定</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { Get, Post, Put, Delete } from '@/api/cms/thirdaccount'

import qs from 'qs'
import Single_wpaccount_select from './elements/single_wpaccount_select'

const orderMapper = {
  'ascending': 0,
  'descending': 1,
  'null': ''
}

export default {
  components: { Single_wpaccount_select },
  data() {
    return {
      title: '新增配置',
      listLoading: false,
      dialogVisible: false,
      temp: {
        wpaccount_id: '',
        username: '',
        password: '',
        plantform: '',
        token: '',
        today_post: 0,
        daily_post: 10
      },
      kwInput: '',
      action: '',
      data: [],
      order_by: '',
      order_desc: '',
      // pagination
      pagesize: 10,
      page: 1,
      total: 0,
      plantforms: [
        { 'value': 'quora.com' },
        { 'value': 'reddit.com' },
        { 'value': 'linkedin.com' },
        { 'value': 'pinterest.com' },
        { 'value': 'facebook.com' },
        { 'value': 'twitter.com' },
        { 'value': 'medium.com' },
        { 'value': 'tumblr.com' },
        { 'value': 'wordpress.com' },
        { 'value': 'blogger.com' },
        { 'value': 'tiktok.com' },
        { 'value': 'youtube.com' }
      ]
    }
  },
  created() {
    this.getList()
  },
  methods: {
    createFilter(queryString) {
      return (restaurant) => {
        return (restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0)
      }
    },
    querySearchPlantform(queryString, cb) {
      var restaurants = this.plantforms
      var results = queryString ? restaurants.filter(this.createFilter(queryString)) : restaurants
      // 调用 callback 返回建议列表的数据
      cb(results)
    },
    resetTemp() {
      this.temp = {
        wpaccount_id: '',
        username: '',
        password: '',
        token: '',
        today_post: 0,
        daily_post: 10
      }
    },
    handleAction(action, row) {
      this.action = action
      switch (action) {
        case 'create':
          this.dialogVisible = true
          this.title = '添加新账号'
          this.resetTemp()
          break
        case 'edit':
          for (const x in row) {
            this.temp[x] = row[x]
          }
          this.dialogVisible = true
          break
        case 'delete':
          this.dialogVisible = true
          this.title = `确认要删除${row.username}?`
          this.temp = row
          break
      }
    },
    changeWPAccount(v) {
      this.temp.wpaccount_id = v
    },
    confirm() {
      this.temp.project_id = this.$store.getters.projectId
      const _data = this.temp
      switch (this.action) {
        case 'create':
          Post(_data).then(response => {
            this.dialogVisible = false
            this.getList()
            this.notify('添加成功')
          })
          return
        case 'edit':
          Put(_data.id, _data).then(response => {
            this.dialogVisible = false
            this.getList()
            this.notify('更新成功')
          })
          return
        case 'delete':
          Delete(_data.id).then((response) => {
            this.dialogVisible = false
            this.getList()
            this.notify('删除成功')
          })
          return
      }
    },
    notify(message, duration) {
      this.$notify({
        title: '成功',
        message: message,
        type: 'success',
        duration: duration || 2000
      })
    },
    getList() {
      this.listLoading = true
      Get(null, qs.stringify({
        kw: this.kwInput,
        project_id: this.$store.getters.projectId,
        pagesize: this.pagesize,
        page: this.page,
        order_by: this.order_by,
        order_desc: this.order_desc
      }))
        .then(response => {
          this.data = response.data.data
          this.listLoading = false
          this.total = response.data.total
        }).catch(e => {
          this.listLoading = false
        })
    },
    // pageSize 改变后触发 table 更新
    handleSizeChange(val) {
      this.pagesize = val
      this.getList()
    },
    // page 改变后触发 table 更新
    handlePageChange(val) {
      this.page = val
      this.getList()
    },
    // 点击搜索
    handleSearch() {
      this.page = 1
      this.getList()
    },
    handleSort(v) {
      this.order_by = v.prop
      this.order_desc = orderMapper[v.order]
      this.getList()
    }
  }
}
</script>

<style scoped>

.el-header, .el-footer {
  background-color: #fff;
  color: #333;
  text-align: left;
  line-height: 60px;
}

.el-aside {
  background-color: #fff;
  border-left: 1px solid #ebeef5;
  color: #333;
  padding-top: 20px;
  padding-left: 10px;
  line-height: 250px;
}

.el-main {
  background-color: #fff;
  color: #333;
  text-align: center;
}

body > .el-container {
  margin-bottom: 40px;
}

.el-container:nth-child(5) .el-aside,
.el-container:nth-child(6) .el-aside {
  line-height: 260px;
}

.el-container:nth-child(7) .el-aside {
  line-height: 320px;
}

.el-tag + .el-tag {
  margin-left: 10px;
}

/*.top {*/
/*padding-left: 20px;*/
/*padding-right: 20px;*/
/*margin-bottom: 5px;*/
/*margin-top: 28px;*/
/*}*/

/*.head {*/
/*width: calc(20% - 70px);*/
/*}*/

/*.web {*/
/*margin-top: 10px;*/
/*margin-bottom: 10px;*/
/*display: inline-block;*/
/*width: 100%;*/
/*}*/

.web >>> .el-input {
  min-width: 1200px;
}

.app-container >>> .el-dialog {
  margin-top: 48px !important;
  margin-bottom: 68px;
}

.mt-detail:hover, .svg-icon:hover {
  cursor: pointer;
}

.mt-detail {
  color: #1976d2;
}

.mt-dialog >>> .el-dialog {
  margin: 88px auto !important;
}

.clearfix ::after {
  content: "";
  clear: both
}

.mt-table {
  color: black;
  padding: 20px;
}

.mt-pagination {
  float: right;
  margin-top: 10px;
}

.mt-top {
  margin: 15px 0;
}

.mt-row-btn {
  float: right;
  vertical-align: middle;
  color: white;
}

.mt-btn {
  background-color: rgba(0, 0, 0, 0);
  background-image: none;
  border-radius: 1000px;
  border-bottom-style: solid;
  border-bottom-width: 1px;
  border-collapse: collapse;
  border-image-outset: 0px;
  border-image-repeat: stretch;
  border-image-slice: 100%;
  border-image-source: none;
  border-image-width: 1;
  border-left-style: solid;
  border-left-width: 1px;
  border-right-style: solid;
  border-right-width: 1px;
  border-top-style: solid;
  border-top-width: 1px;
  box-sizing: border-box;
  color: #46be8a;
  cursor: pointer;
  display: block;
  float: right;
  font-family: "Helvetica Neue", Helvetica, Tahoma, Arial, "Microsoft Yahei", "Hiragino Sans GB", "WenQuanYi Micro Hei", sans-serif;
  font-size: 14px;
  font-weight: 400;
  height: 26px;
  hyphens: manual;
  line-height: 14px;
  margin-bottom: 0px;
  margin-right: 10px;
  padding: 5px;
  text-align: center;
  text-decoration-color: rgb(82, 96, 105);
  text-decoration-line: none;
  text-decoration-style: solid;
  text-rendering: optimizeLegibility;
  text-size-adjust: 100%;
  touch-action: manipulation;
  transition-delay: 0s, 0s, 0s, 0s;
  transition-duration: 0.2s, 0.2s, 0.2s, 0.2s;
  transition-property: border, color, width, background-color;
  transition-timing-function: linear, linear, linear, linear;
  user-select: none;
  vertical-align: middle;
  visibility: visible;
  white-space: nowrap;
  background: white;
}

.mt-btn:hover + >>> .mt-icon {
  color: #fff;
}

.mt-btn:hover {
  background: #46be8a;
}

.danger:hover {
  background-color: #F56C6C;
}

.danger {
  color: #F56C6C;
}

.default:hover {
  background-color: #62a8ea;
}

.default {
  color: #62a8ea;
}

.edit:hover {
  background-color: #909399;
}

.edit {
  color: #909399;
}

.mt-row-title {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  width: calc(100% - 220px);
  display: inline-block;
  vertical-align: middle;
}

.mt-row-btns {
  float: right;
  margin-right: 10px;
  z-index: 10000;
}

</style>
